<div class="layui-card-header">
    <h2 class="header-title">修改密码</h2>
</div>
<div class="layui-card-body" style="padding: 30px;">
    <form class="layui-form" id="form-psw">
        <input type="hidden" name="id" id="id">
        <div class="layui-form-item">
            <input type="password" name="oldPassword" required lay-verify="required|psw" class="layui-input"
                   placeholder="请输入原密码" lay-verType="tips">
        </div>
        <div class="layui-form-item">
            <input type="password" name="newPassword" required lay-verify="required|psw" class="layui-input"
                   placeholder="请输入新密码" lay-verType="tips">
        </div>
        <div class="layui-form-item">
            <input type="password" name="rePassword" required lay-verify="required|repsw" class="layui-input"
                   placeholder="再次输入新密码" lay-verType="tips">
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="submit-psw">确认修改</button>
        </div>
    </form>
</div>

<script>
layui.use(['layer','form', 'admin','config'], function () {
    var form = layui.form;
    var admin = layui.admin;
    var config = layui.config;
    var layer = layui.layer;
    var user = config.getUser();
    
    console.log(user.id);
    $("#id").val(user.id);
    //监听提交
    form.on('submit(submit-psw)', function (data) {
        layer.load(2);
        admin.req('api-user/users/password',JSON.stringify(data.field), function (data) {
            layer.closeAll('loading');
            if (data.resp_code == 0) {
                admin.closePopupRight();
                layer.msg(data.resp_msg, {icon: 1}, function () {
                    config.removeToken();
                    location.replace('login.html');
                });
            } else {
                layer.msg(data.resp_msg, {icon: 2});
            }
        }, 'PUT');
        return false;
    });
    
    //添加表单验证方法
    form.verify({
        psw: [/^[\S]{4,12}$/, '密码必须4到12位，且不能出现空格'],
        repsw: function (t) {
            if (t !== $('#form-psw input[name=newPassword]').val()) {
                return '两次密码输入不一致';
            }
        }
    });
});
</script>